Ext.define("App.datacollection.view.CollectionConfigurationEditor", {
    extend: 'Extend.form.Panel',
    alias: ['widget.CollectionConfigurationEditor'],
    controller: 'CollectionConfigurationEditorController',
    url: 'collection_configuration/save',
    entity: 'entity',
    config: {resetBySave: false},
    scrollable: false,
    layout: 'border',
    buttons: {
        style: 'background:#f1f1f1;',
        items: ['->', {
            text: '保存',
            handler: function () {
                this.up('CollectionConfigurationEditor').save();
            }
        }, {
            text: '重置',
            handler: function () {
                this.up('CollectionConfigurationEditor').reset();
            }
        }]
    },
    listeners: {
        afterrender: 'onAfterrender',
        save: 'onSaved'
    },
    items: [
        {
            xtype: 'tabpanel',
            region: 'center',
            itemId: 'collectionGroupTab',
            columnWidth: 1, padding: 0, margin: 0,
            items: [{
                xtype: 'panel',
                hidden: 'true',
                items: [
                    {
                        xtype: 'hidden',
                        name: 'id'
                    },
                    {
                        xtype: 'hidden',
                        name: 'project.id'
                    }
                ]
            }]
        }
    ]
});

Ext.define('App.datacollection.view.CollectionConfigurationEditorController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.CollectionConfigurationEditorController',
    onAfterrender: function () {
        if (Ext.isEmpty(app.project) || Ext.isEmpty(app.project.id)) {
            return;
        }
        var me = this;
        var view = this.getView();
        Ext.defer(function () {
            view.mask('加载...');
            Ext.Ajax.request({
                url: 'collection_configuration/get_by_projectid',
                params: {pid: app.project.id},
                success: function (resp) {
                    if (resp.result.success == true) {
                        var collectionConfiguration = resp.result.data;
                        me.setCollectionConfigurationToForm(collectionConfiguration);
                        var collectionGroups = collectionConfiguration.collectionGroups;
                        me.createCollectionGroupPanels(collectionGroups);

                        // me.setCollectionGroupsToForm(collectionGroups)
                    }
                },
                callback: function () {
                    view.unmask();
                }
            });
        }, 100);
    },
    setCollectionConfigurationToForm: function (collectionConfiguration) {
        this.getView().down('hidden[name="id"]').setValue(collectionConfiguration.id);
        this.getView().down('hidden[name="project.id"]').setValue(collectionConfiguration.projectId);
    },
    createCollectionGroupPanels: function (collectionGroups) {
        var me = this;
        var tab = me.getView().down('[itemId="collectionGroupTab"]');
        for (var i = 0; i < collectionGroups.length; i++) {
            var collectionGroup = collectionGroups[i];
            tab.insert(i, Ext.create({
                xtype: 'panel',
                scrollable: true,
                title: collectionGroup.dataTable.alias,
                layout: 'column',
                margin: '10 0 10 0',
                collectionGroup: collectionGroup,
                collectionGroupIndex: i,
                listeners: {
                    afterrender: function () {
                        me.initCollectionFields(this);
                    }
                }
            }));
        }
        tab.setActiveItem(0);
    },
    initCollectionFields: function (view) {
        var collectionGroup = view.collectionGroup;
        var collectionGroupIndex = view.collectionGroupIndex;
        var collectionGroupsId = Ext.create({xtype: 'hidden', name: 'collectionGroups[' + collectionGroupIndex + '].id', value: collectionGroup.id});
        var dataTableId = Ext.create({xtype: 'hidden', name: 'collectionGroups[' + collectionGroupIndex + '].dataTable.id', value: collectionGroup.dataTable.id});
        view.add(collectionGroupsId);
        view.add(dataTableId);
        var collectionFields = collectionGroup.collectionFields;
        this.createCollectionFields(view, collectionFields, collectionGroupIndex);
    },
    createCollectionFields: function (view, collectionFields, pi) {
        var me = this;
        for (var i = 0; i < collectionFields.length; i++) {
            var cf = collectionFields[i];

            var inputTypeCombobox = Ext.create({
                xtype: 'combobox',
                fieldLabel: cf.tableField.alias,
                displayField: 'name',
                allowBlank: false,
                blankText: cf.tableField.alias + '为必填字段，不能为空！',
                margin: '10 0 0 40',
                valueField: 'value',
                editable: false,
                columnWidth: .25,
                queryMode: 'local',
                labelAlign: 'top',
                value: cf.inputType,
                name: 'collectionGroups[' + pi + '].collectionFields[' + i + '].inputType',
                store: {
                    fields: ['name', 'value'],
                    data: [{name: '手录', value: 1},
                        {name: '采集', value: 2},
                        {name: '手录+采集', value: 3}]
                },
                listeners: {
                    change: function ($this, nv, ov) {
                        if (nv == 2 || nv == 3) {
                            $this.nextNode().enable();
                        } else {
                            $this.nextNode().disable();
                            $this.nextNode().setValue()
                        }
                    }
                }
            });

            var collectionSourceCombobox = Ext.create({
                xtype: 'combobox',
                fieldLabel: '采集任务',
                itemId: cf.tableField.id + '_ct',
                displayField: 'name',
                allowBlank: false,
                blankText: '采集任务为必填字段，不能为空！',
                margin: '10 0 0 22',
                valueField: 'value',
                editable: false,
                columnWidth: .25,
                queryMode: 'local',
                labelAlign: 'top',
                value: cf.collectionSource,
                disabled: cf.inputType == 1,
                name: 'collectionGroups[' + pi + '].collectionFields[' + i + '].collectionSource',
                store: {
                    fields: ['name', 'value'],
                    data: [{name: '任务1', value: 1},
                        {name: '任务2', value: 2},
                        {name: '任务3', value: 3}]
                }
            });

            var collectionFieldsId = Ext.create({xtype: 'hidden', name: 'collectionGroups[' + pi + '].collectionFields[' + i + '].id', value: cf.id});
            var tableFieldId = Ext.create({xtype: 'hidden', name: 'collectionGroups[' + pi + '].collectionFields[' + i + '].tableField.id', value: cf.tableField.id});
            view.add(collectionFieldsId);
            view.add(tableFieldId);
            view.add(inputTypeCombobox);
            view.add(collectionSourceCombobox);
        }
    },
    onSaved: function () {
        Extend.Msg.info('提示', '采集配置信息已经修改成功！');
    }
});